//
//  AddUserGenderCell.swift
//  koolcar
//
//  Created by xiantian on 2023/9/20.
//

import UIKit

typealias AddUserGenderCellBlock = (NSInteger) -> Void
class AddUserGenderCell: UITableViewCell {
    var titleLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    var callBack: AddUserGenderCellBlock?
    var segmentBtn: UISegmentedControl!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(titleLab)
        
        segmentBtn = UISegmentedControl(items: [
            MMLocalizedString("add_gender_Mrs", comment: "女士"),
            MMLocalizedString("add_gender_Mr", comment: "先生"),
            MMLocalizedString("add_gender_Miss", comment: "小姐")
        ])
        segmentBtn.backgroundColor = .white
        segmentBtn.setBackgroundImage(UIImage.init(color: .white), for: .normal, barMetrics: .default)
        segmentBtn.setBackgroundImage(UIImage.init(color: Theme.mainColor), for: .selected, barMetrics: .default)
        segmentBtn.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.titleLabColor, NSAttributedString.Key.font: UIFont.init(name: Theme.PingFangSCMedium, size: 14)!], for: .normal)
        segmentBtn.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.whiteColor, NSAttributedString.Key.font: UIFont.init(name: Theme.PingFangSCMedium, size: 14)!], for: .selected)
        segmentBtn.setDividerImage(UIImage.init(color: .white), forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default)
        segmentBtn.tintColor = Theme.mainColor
        segmentBtn.layer.borderWidth = 1
        segmentBtn.layer.cornerRadius = 4
        segmentBtn.layer.borderColor = Theme.tfBorderColor.cgColor
        segmentBtn.layer.masksToBounds = true
        segmentBtn.addTarget(self, action: #selector(self.segmentBtnAction), for: .valueChanged)
        segmentBtn.selectedSegmentIndex = 0
        contentView.addSubview(segmentBtn)
        self.addSnpLayout()
    }
    
    func addSnpLayout(){
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.height.greaterThanOrEqualTo(0)
            make.right.equalTo(-14)
            make.top.equalTo(0)
        }
        segmentBtn.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.top.equalTo(titleLab.snp.bottom).offset(8)
            make.height.equalTo(44)
            make.bottom.equalTo(-14).priority(.high)
        }
        self.layoutIfNeeded()
        self.setSegmentControlRadius()
    }
    
    ///设置圆角
    private func setSegmentControlRadius(){
        self.segmentBtn.subviews.forEach { aView in
            aView.layer.cornerRadius = 8
            aView.layer.masksToBounds = true
        }
    }
    
    ///切换seg按钮
    @objc func segmentBtnAction(){
        self.callBack?(self.segmentBtn.selectedSegmentIndex)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showTopTitleLab(){
        let tempStr1 = String(format: "*%@", MMLocalizedString("profile_salutation", comment: "称谓"))
        let attr1 = NSMutableAttributedString.init(string: tempStr1)
        attr1.addAttributes([NSAttributedStringKey.foregroundColor: Theme.redColor], range: NSRange.init(location: 0, length: 1))
        self.titleLab.attributedText = attr1
        
        self.titleLab.snp.updateConstraints { make in
            make.height.greaterThanOrEqualTo(22)
        }
        self.updateConstraints()
    }
}

